package com.yoda.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.youdianda.domain.YddAdvimg;
import com.yoda.youdianda.domain.YddAdvpos;
import com.yoda.youdianda.mapper.YddAdvposMapper;
import com.yoda.youdianda.service.YddAdvimgService;
import com.yoda.youdianda.service.YddAdvposService;
import com.yoda.youdianda.util.PageCopyUtil;
import com.yoda.youdianda.vo.PageResult;
import com.yoda.youdianda.vo.YddAdvposVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements YddAdvposService {
    @Autowired
    private YddAdvposMapper advposMapper;

    @Autowired
    private YddAdvimgService yddAdvimgService;

    @Override
    public Integer countAdvpos() {
        return advposMapper.selectCount(new LambdaQueryWrapper<>());
    }


    @Override
    public PageResult<YddAdvposVo> getPage(String advposdesc, String advposname, int page, int limit) {
        //创建分页对象
        Page<YddAdvpos> advposGetPage = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        //模糊查询,非空校验
        wrapper.like(StringUtils.isNotEmpty(advposdesc), YddAdvpos::getAdvposdesc, advposdesc);
        wrapper.like(StringUtils.isNotEmpty(advposname), YddAdvpos::getAdvposname, advposname);

        //获取Page对象
        Page<YddAdvpos> advposPage = this.page(advposGetPage, wrapper);
        //非空校验
        if (advposPage == null) {
            return null;
        }

        //获取Page中数据
        List<YddAdvpos> yddAdvposList = advposPage.getRecords();

        //创建返回自定义Page
        PageResult<YddAdvposVo> yddAdvposPageResult = new PageResult<>();

        //page复制,忽略data数据
        PageCopyUtil.pageCopy(advposPage, yddAdvposPageResult);

        //创建返回数据集合
        List<YddAdvposVo> yddAdvposVoList = new ArrayList<>();

        //完成数据库数据向VO数据的转换
        for (YddAdvpos yddAdvpos : yddAdvposList) {
            //创建vo对象用于临时存储
            YddAdvposVo yddAdvposVo = new YddAdvposVo();
            //复制
            BeanUtils.copyProperties(yddAdvpos, yddAdvposVo);
            //设置不可复制内容
            yddAdvposVo.setDefault_data(yddAdvpos.getDefaultData());

            LambdaQueryWrapper<YddAdvimg> imgIdWrapper = new LambdaQueryWrapper<>();
            //设置条件,广告位Id主键于img的advimgpos对应
            imgIdWrapper.eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId());
            //计算广告位图片数
            int count = yddAdvimgService.count(imgIdWrapper);
            yddAdvposVo.setAdvposnum(count);
            //添加到集合
            yddAdvposVoList.add(yddAdvposVo);
        }
        //设置返回数据
        yddAdvposPageResult.setData(yddAdvposVoList);
        return yddAdvposPageResult;
    }

    //修改回显
    @Override
    public YddAdvposVo getYddadvposVo(Long id) {
        YddAdvpos yddAdvpos = this.getById(id);
        if (yddAdvpos == null) {
            return null;
        }
        YddAdvposVo yddAdvposVo = new YddAdvposVo();
        BeanUtils.copyProperties(yddAdvpos, yddAdvposVo);
        yddAdvposVo.setDefault_data(yddAdvpos.getDefaultData());
        return yddAdvposVo;
    }

    //删除
    @Override
    public boolean delById(int id) {
        this.removeById(id);
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvimg::getAdvimgpos, id);
        List<YddAdvimg> list = yddAdvimgService.list(wrapper);
        return list != null;
    }

    @Override
    public boolean addAdvPos(YddAdvpos yddAdvpos) {
        int count = advposMapper.insert(yddAdvpos);
        return count>0;
    }
}
